Back to Portfolio
#Browser Caching#Redis#Pagination

Exploring API Performance Optimization - Caching, Query Efficiency & Pagination

U
Utkarsh
Jun 7, 2026
7 min read
Week 3

Browser Caching

So one of the best place to cache any web data is in the tool itself we use the most to view that data i.e. the web browser

How It Works

Case 1: When the user is visiting a site for the first time

Initially browsers don't have any cache, its after their 1st request to the website that they get the data and are able to store it

Before cache stats So here we can see that 3.71mb of data was transferred

Case 2: When the user has visited the before

In this case the browser will retrieve the HTML page from the web server but consult its cache for the static assets (JavaScript, CSS, images).

After Cache stats

As you can see the transferred data drops from 3.71mb to 34.24kb which is around 100x lesser than the initial page load The transferred columns shows all the files that were cached by the browser after the 1st visit

How does the browser know what to cache??

So in the last blog i learned that we should be very careful with what and how much are we caching, cause neither do we want any user sensitive data to be cached in browser nor do we want to cache every other fill, as security and storage management is very important.

Well its not like the browser automatically understand what is important to cache and what is not.

The browser inspects the headers of the HTTP response generated by the web server. There are four headers commonly used for caching:

  • ETag
  • Cache-Control
  • Expires
  • Last-Modified

Let's discuss about each one of them one by one

ETag

ETag is a string that can be used to validate cache(check whether the stored data is still fresh or not). Its usually a hash of the file content

The server can include an ETag in its response, which can be used by the browser in the future(after the file has expired) to check whether the cache contains a stale copy or not.

If the hash is same, then the resource hasn’t changed and the server responds with a 304 response code (Not Modified) with an empty body. This lets the browser know it’s still safe to use the cached copy.

Cache-Control

The Cache-Control header has a lot of values we can set to control how the data is cached, validated and expired in the browser. These can also be used in combination

Cache Behavior

Cache-Control: Public

It tells the browser and other intermediate caches(like CDN) that the resource can be stored and shared by multiple users. It is typically used for static, non-personalized assets(like images, public pdfs), making it easier to reduce bandwidth and speed up load times.

Cache-Control: Private

It tells the cache storages that the response is only meant for a specific user and not for public shared caches(like CDNs, proxies etc). It is typically used cache dynamic responses tailored specifically to the logged-in user (e.g., displaying a username) or pages displaying account details, order history, or user dashboards.

Cache-Control: no-store

It is used to prevent browsers, content delivery networks (CDNs), and intermediate proxy servers from saving a copy of a web resource. It is used to enures privacy and security by forcing every user request to download directly from the origin server.

Cache-Control: no-cache

It allows a browser or intermediary cache to store the requested resource, but strictly forces them to validate it with the origin server before reuse. Contrary to its name, no-cache does not mean "do not cache"; instead, it means "do not reuse without revalidation".

How it Works (The Revalidation Process): When a browser encounters a no-cache directive, it manages the resource through a conditional validation handshake

  1. Storage: The browser saves a copy of the resource locally.
  2. The Next Request: Instead of loading the file instantly from disk, the browser sends a lightweight request to your server to check if the file has changed.
  3. The Server Check: The server uses validator headers like ETag or Last-Modified to verify the asset.
  4. The Response:
    • If the file has not changed, the server responds with a quick 304 Not Modified. The browser safely uses its stored copy without downloading the entire payload again.
    • If the file has changed, the server returns a 200 OK along with the brand-new file.
Cache-Control: immutable

It tells the browser that a resource will never change during its freshness lifetime. When a user manually reloads or refreshes a web page, browsers normally send a conditional network request (using If-None-Match or If-Modified-Since) to the origin server to check if static assets have changed. Adding the immutable flag instructs the browser to completely bypass this revalidation step. The browser serves the file directly from its local disk cache, preventing unnecessary server round-trips and drastically speeding up page reloads.

Expiration

Cache-Control: max-age=60

This tell the browser and any other intermediary cache(like CDNs) to cache the data for a specific amount of time in seconds(60 i.e. 1min).

Cache-Control: s-max-age=60

This is only applied to intermediary cache and not the browser cache Note: If there is max-age and s-max-age, the CDNs will ignore the max-age and apply the s-max-age

Validation

Cache-Control: must-validate

It ensures that caches must not serve a stale(outdated) resource without first checking with the origin server.

How must-revalidate Works

  1. While Fresh: Caches can freely serve the resource without contacting the origin server.
  2. When Stale: The cache must contact the server to validate the data. It typically does this using a conditional request header (e.g., If-None-Match with an ETag or If-Modified-Since).
  3. If the Server Says Yes: The server returns a 304 Not Modified, allowing the cache to refresh its local entry and serve it to the user without re-downloading the body.
  4. If the Server Says No: The server returns the updated content.

Expires

It specifies a specific date and time after which a cached resources is considered stale.

Expires: Wed, 11 Jun 2027 16:00:00 GMT

So why was max-age or s-max-age is used when we have this?? Its because it relies on absolute dates which means that if the server's clock and the user's device clock are out of sync, caching errors can occur

Note: If a response contains both a Cache-Control: max-age=... header and an Expires header, modern browsers completely ignore the Expires header.

Last-Modified

This field contains the date and time the resource was last modified. It serves as a validator to help determine if a stored copy of a file is still up to date

Last-Modified: Mon, 11 Jun 2026 14:45:00 GMT

Examples

Let's look at some example using all these tags together:

Cache-Control: public, max-age=31536000, immutable
ETag: "34aa930-5f212"

How They Work Together

  • public: Tells both the browser and shared caches (like CDNs) that they are allowed to store this asset.
  • max-age=31536000: Sets the expiration duration to exactly 1 year (31,536,000 seconds).
  • immutable: Tells the browser that the file content will never change. This prevents the browser from sending conditional revalidation requests to the server even when a user explicitly refreshes the page.
  • ETag: Serves as a fallback fingerprint. If the cache ever clears or expires, the browser will pass this hash to the server to verify the asset without re-downloading it.
Cache-Control: public, no-cache, must-revalidate
ETag: "w/67ab9201f"
Last-Modified: Wed, 10 Jun 2026 12:00:00 GMT

How They Work Together

  • no-cache: It tells the browser it can store the file locally, but it must check with the origin server for approval before using it.
  • must-revalidate: Reinforces that once a resource becomes stale, it absolutely cannot be served from cache without server validation.
  • ETag & Last-Modified: The server passes these validators to the client. On the next page load, the browser sends an If-None-Match: "w/67ab9201f" request. If the file hasn't changed, the server responds instantly with a lightweight 304 Not Modified payload instead of a heavy 200 OK download.

Note: Without a max-age directive, the cache considers this response immediately stale and is instructed to never serve it without first revalidating it with the origin server.

Redis

U

Utkarsh

Full-Stack Developer • Backend & AI